package com.zhku.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhku.base.exception.TaoTaoException;
import com.zhku.base.model.PageParams;
import com.zhku.base.model.PageResult;
import com.zhku.base.model.RestResponse;
import com.zhku.shop.feignclient.UserClient;
import com.zhku.shop.mapper.TtShopMapper;
import com.zhku.shop.model.dto.QueryShopParamsDto;
import com.zhku.shop.model.dto.ShopDto;
import com.zhku.shop.model.dto.UpdateUserRoleDto;
import com.zhku.shop.model.po.TtShop;
import com.zhku.shop.model.po.TtUser;
import com.zhku.shop.service.TtShopService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.transform.Source;
import java.time.LocalDateTime;
import java.time.Year;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TtShopServiceImpl extends ServiceImpl<TtShopMapper, TtShop> implements TtShopService {

    @Autowired
    TtShopMapper ttShopMapper;

    @Autowired
    UserClient userClient;

    @Transactional
    @Override
    public TtShop createShop(ShopDto shopDto) {
        String shopName = shopDto.getName();
        if (StringUtils.isEmpty(shopName)) {
            throw new TaoTaoException("传入的商家名称不可为空");
        }
        LambdaQueryWrapper<TtShop> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TtShop::getName,shopName);
        TtShop shop = ttShopMapper.selectOne(queryWrapper);
        if (shop != null) {
            throw new TaoTaoException("当前商家名称已存在");
        }
        TtShop ttShop = new TtShop();
        BeanUtils.copyProperties(shopDto,ttShop);
        String id = UUID.randomUUID().toString();
        ttShop.setId(id);
        int insert = ttShopMapper.insert(ttShop);
        if (insert <= 0) {
            throw new TaoTaoException("创建新的店铺信息失败");
        }
        return ttShop;
    }

    @Transactional
    @Override
    public void deleteShop(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new TaoTaoException("传入的店铺id为空");
        }
        //将属于该店铺的所有员工先修改其角色为普通用户
        List<TtUser> users = userClient.shopUserList(id);
        if (users.size() != 0){
            UpdateUserRoleDto updateUserRoleDto = new UpdateUserRoleDto();
            for (TtUser user : users) {
                updateUserRoleDto.setUserId(user.getId());
                //当该员工修改为普通用户
                updateUserRoleDto.setRoleId("3");
                RestResponse restResponse = userClient.updateUserRole(updateUserRoleDto);
                if (restResponse.getCode() == -1){
                    throw new TaoTaoException("远程调用修改用户角色失败");
                }
                RestResponse<TtUser> ttUserRestResponse = userClient.updateUserShop(user.getId());
                if (ttUserRestResponse.getCode() == -1){
                    throw new TaoTaoException("远程调用修改用户店铺为空失败");
                }
            }
        }
        int i = ttShopMapper.deleteById(id);
        if (i <= 0) {
            throw new TaoTaoException("删除店铺失败");
        }
    }

    @Transactional
    @Override
    public TtShop updateShop(TtShop ttShop) {
        String id = ttShop.getId();
        TtShop ttShopOld = ttShopMapper.selectById(id);
        if (ttShopOld == null) {
            throw new TaoTaoException("当前店铺不存在");
        }
        int i = ttShopMapper.updateById(ttShop);
        if (i <= 0){
            throw new TaoTaoException("修改店铺信息失败");
        }
        return ttShop;
    }

    @Override
    public TtShop getShop(String id) {
        if (StringUtils.isEmpty(id)){
            throw new TaoTaoException("传入空的店铺地址id");
        }
        TtShop ttShop = ttShopMapper.selectById(id);
        if (ttShop == null) {
            throw new TaoTaoException("店铺不存在");
        }
        return ttShop;
    }


    @Override
    public PageResult<TtShop> queryShopList(PageParams pageParams, QueryShopParamsDto queryShopParamsDto) {
        LambdaQueryWrapper<TtShop> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户名进行模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(queryShopParamsDto.getName()),
                TtShop::getName, queryShopParamsDto.getName());
        queryWrapper.like(StringUtils.isNotEmpty(queryShopParamsDto.getIntro()),
                TtShop::getIntro, queryShopParamsDto.getIntro());
        //创建page分页参数对象，参数：当前页码数，每页记录数
        Page<TtShop> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        Page<TtShop> ttShopPage = ttShopMapper.selectPage(page, queryWrapper);
        List<TtShop> items = ttShopPage.getRecords();
        long counts = ttShopPage.getTotal();
        PageResult<TtShop> ttShopPageResult = new PageResult<>(items,
                counts, pageParams.getPageNo(), pageParams.getPageSize());
        return ttShopPageResult;
    }

    @Override
    public TtShop shopByName(String name) {
        if (StringUtils.isEmpty(name)) {
            throw new TaoTaoException("传入的店铺名称为空");
        }
        LambdaQueryWrapper<TtShop> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TtShop::getName,name);
        TtShop ttShop = ttShopMapper.selectOne(queryWrapper);
        if (ttShop == null) {
            return null;
        }
        return ttShop;
    }
}
